package com.example.pingapplication2.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.pingapplication2.R;
import com.example.pingapplication2.config.Config;
import com.example.pingapplication2.databinding.FragmentCountDownBinding;
import com.example.pingapplication2.net.INetListener;
import com.example.pingapplication2.net.RetrieveFeedTask;
import com.example.pingapplication2.viewModel.CountDownViewModel;
import com.example.pingapplication2.viewModel.StateViewModel;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 倒计时为主要特点，首先设置计时时间，记录ping的丢包率。
 */
public class CountDownFragment extends Fragment implements INetListener {
    FragmentCountDownBinding fragmentCountDownBinding;
    StateViewModel mStateViewModel;
    CountDownViewModel mCountDownViewModel;

    RetrieveFeedTask Instance ;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Instance = RetrieveFeedTask.getInstance(getContext());
        Instance.registerListener((INetListener)this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_count_down, container, false);
        fragmentCountDownBinding = FragmentCountDownBinding.bind(view);

        //viewModel的初始化
        mStateViewModel = new ViewModelProvider(requireActivity()).get(StateViewModel.class);
        mCountDownViewModel = new ViewModelProvider(requireActivity()).get(CountDownViewModel.class);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        fragmentCountDownBinding.timer.setCurTime(mCountDownViewModel.curTime.getValue());

        fragmentCountDownBinding.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavController controller = Navigation.findNavController(view);
                controller.navigate(R.id.action_countDownFragment_to_instrumentFragment);
            }
        });

        fragmentCountDownBinding.setup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(fragmentCountDownBinding.editText.getText().toString().equals("")) {
                    Toast.makeText(getContext(), "请输入测试时间！", Toast.LENGTH_LONG).show();
                }else if(mCountDownViewModel.start.getValue()){
                    Toast.makeText(getContext(), "运行期间，不要重新设置！",Toast.LENGTH_LONG).show();
                }else{
                    int time = Integer.valueOf(fragmentCountDownBinding.editText.getText().toString());
                    mCountDownViewModel.set.postValue(true);
                    Log.d("time",String.valueOf(time));
                    fragmentCountDownBinding.timer.setUpTimer(time);
                }
            }
        });

        fragmentCountDownBinding.start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!mCountDownViewModel.set.getValue()){
                    Toast.makeText(getContext(), "请先设置！", Toast.LENGTH_LONG).show();
                }else if(mStateViewModel.flag.getValue() != 0){
                    Toast.makeText(getContext(), "当前正在运行过程中，请稍后再试！", Toast.LENGTH_LONG).show();
                }else {
                    mStateViewModel.flag.postValue(1);//countdown模式
                    mCountDownViewModel.curTime.postValue(Integer.valueOf(fragmentCountDownBinding.editText.getText().toString()));
                    mCountDownViewModel.start.postValue(true);
                    mStateViewModel.packetNum.postValue(0);
                    mStateViewModel.failureNum.postValue(0);
                    mStateViewModel.successNum.postValue(0);
                }
            }
        });



        mCountDownViewModel.curTime.observe(getActivity(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                Log.d("CountDownCurTime", String.valueOf(integer));
                int time = mCountDownViewModel.curTime.getValue();
                if(time >= 0){
                    fragmentCountDownBinding.timer.setCurTime(time);
                }else if(mCountDownViewModel.countDownRunTimer != null){
                    mCountDownViewModel.countDownRunTimer.cancel();
                    mCountDownViewModel.countDownPingTimer.cancel();
                    Log.d("timer2","cancel");
                    mCountDownViewModel.start.postValue(false);
                    mStateViewModel.flag.postValue(0);
                }

            }
        });

        mStateViewModel.packetLoss.observe(getViewLifecycleOwner(), new Observer<Double>() {
            @Override
            public void onChanged(Double aDouble) {
                Log.d("packetLoss",String.valueOf(aDouble));
                fragmentCountDownBinding.diuPacket.setText(String.valueOf(aDouble));
                if(aDouble > 90){
                    fragmentCountDownBinding.image.setImageResource(R.drawable.alarmon);
                }else {
                    fragmentCountDownBinding.image.setImageResource(R.drawable.alarmoff);
                }
            }
        });
    }



    @Override
    public void pingSuccess() {

    }

    @Override
    public void pingFailure() {

    }
}